<template>
  <div class="container">
    <div class="container_right">
      <div class="title">
        <div>请登录</div>
      </div>
      <div>
        <div class="input1">
          账号：
          <el-input v-model="userId" type="text" placeholder="请输入账号" style="width: 320px;" />
        </div>

        <div class="input2">
          密码：
          <el-input placeholder="请输入密码" v-model="password" show-password style="width: 320px;"></el-input>
        </div>
      </div>

      <div class="btn">
        <el-button type="primary" plain style="width: 150px;" @click="login">登录</el-button>
        <el-button type="primary" plain style="width: 150px;" @click="resetting">重置</el-button>
      </div>
  
    </div>
  </div>
</template>

<script>
import { login02 } from '@/common/api';




export default {
  name: "login",
  data() {
    return {
      userId: "admin",
      password: "123456"
    };
  },

  methods: {
    
    validateForm() {
      if (this.userId === '') {
        this.$message.error("请输入账号");
        return false;
      } else if (this.password === '') {
        this.$message.error("请输入密码");
        return false;
      }
      return true;
    },
    async login() {
      if (!this.validateForm()) return;

      try {
        const res = await login02(this.userId, this.password);
        console.log('登录响应:', res.data);

        if (res.data.code === 200) {
          const userData = res.data.data;
          localStorage.setItem('token', res.data.data.token);

          this.$message.success(res.data.message);
          this.$router.push("/home");

          console.log("登录成功");
        } else {
          this.$message.error(res.data.message || "登录失败");
        }
      } catch (error) {
        console.error("登录请求出错:", error);
        this.$message.error("登录失败，请重试");
      }
    },
    resetting() {
      this.userId = "";
      this.password = "";
    }
  }
};
</script>

<style scoped>
.container {
  background-image: url('../assets/images/bg.jpg');
  background-size: cover;
  background-position: center;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  justify-content: space-evenly;
}

.container_right {
  margin-top: 40px;
  width: 460px;
  height: 456px;
  background: #fff;
  box-shadow: 0 6px 20px 0 rgba(37, 43, 50, 0.07);
  border-radius: 16px;
  padding: 0 40px;
  border: 1px solid #f9f9f9;
  opacity: 0.75;
}

.title {
  margin-top: 40px;
  font-size: 18px;
  font-weight: 400;
  display: flex;
  justify-content: center;
  font-family: MicrosoftYaHeiSemibold;
}

.input1, .input2 {
  margin-top: 28px;
  display: flex;
  align-items: center;
  font-size: 16px;
}

.btn {
  margin-top: 20px;
  padding: 0 20px;
}

.text {
  margin-top: 28px;
  display: flex;
  justify-content: center;
  font-family: MicrosoftYaHei;
  font-size: 13px;
  color: #626262;
}
</style>
